package ext.acae2.dms.parse;

import java.io.File;
import java.util.List;
import java.util.Map;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathFactory;

import org.apache.log4j.Logger;
import org.w3c.dom.Document;

import wt.log4j.LogR;
import wt.util.WTProperties;
import ext.acae2.dms.bean.DocReferenceLinkBean;
import ext.acae2.dms.bean.DocumentBean;
import ext.acae2.dms.bean.EPMDescribeLinkBean;
import ext.acae2.dms.bean.EPMDocumentBean;
import ext.acae2.dms.bean.EPMMemberLinkBean;
import ext.acae2.dms.bean.EPMReferenceLinkBean;
import ext.acae2.dms.bean.PartBean;
import ext.acae2.dms.bean.PartUsageLinkBean;
import ext.acae2.util.LoadingXmlUtil;
import ext.acae2.util.MappingUtil;
import ext.acae2.util.ParameterUtil;
import ext.acae2.util.UGHostUtil;

public class ParseXml {

	private static final Logger LOGGER = LogR.getLogger(ParseXml.class
			.getName());

	@SuppressWarnings({ "rawtypes" })
	public Map<String, List> execute(String path, String supplier)
			throws Exception {

		LOGGER.debug("<<< Start execute");

		String wthome = (String) WTProperties.getLocalProperties().get("wt.home");
		
		/**before method begin*/
		// load Xml
//		Document doc = LoadingXmlUtil.execute(path);
		
		
		/**after method begin */
		
		org.dom4j.Document doc = LoadingXmlUtil.executeForDom4j(path);
		
		/**after method end */

		String rootFolderPath = path.substring(0,
				path.lastIndexOf(File.separator) + 1);

		// create XPathFactory
		XPathFactory pathFactory = XPathFactory.newInstance();
		// use XPathFactory create XPath
		XPath xpath = pathFactory.newXPath();

		// get mapping
		MappingUtil.execute(wthome + File.separator+"codebase"+File.separator+"ext"+File.separator+"acae2"+File.separator+"dms"+File.separator+"parse" +File.separator+ supplier
				+ "AttributeMap.properties");
		UGHostUtil.execute(wthome + File.separator+"codebase"+File.separator+"ext"+File.separator+"acae2"+File.separator+"dms"+File.separator+"UGServer"+File.separator+"UGHost.properties");

		// parse part
		
		
		/**after method begin cnie*/
		Map<String, List> map = ParseXmlToMap.execute(rootFolderPath,doc,supplier);
		
		/**after method end cnie */
		
		
		/**before method begin*/
		
	/*	Map<String, List> map = ParsePartAndDoc.execute(doc, xpath,
				rootFolderPath, supplier);

		// add partLinkList,epmLinkList,docLinkList to map
		map.put(ParameterUtil.PARTUSAGELINKBEAN,
				ParsePartUsageLink.execute(map.get(ParameterUtil.PARTBEAN)));
		map.put(ParameterUtil.EPMDESCRIBELINKBEAN, ParseEPMDescribeLink
				.execute(map.get(ParameterUtil.PARTBEAN),
						map.get(ParameterUtil.EPMDOCUMENTBEAN)));
		map.put(ParameterUtil.DOCREFERENCELINKBEAN, ParseDocReferenceLink
				.execute(map.get(ParameterUtil.PARTBEAN),
						map.get(ParameterUtil.DOCUMENTBEAN)));*/
		
		LOGGER.debug(">>> End execute");
		//printBean(map);
		return map;
	}

	// Temporary test (remove in the future)
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void printBean(Map<String, List> map) {

		LOGGER.debug("<<< Start printBean");

		List<PartBean> partList = map.get(ParameterUtil.PARTBEAN);
		if(partList != null){
			for (int i = 0; i < partList.size(); i++) {
				LOGGER.debug("！！！ PartBean " + i + " Start ！！！");
				LOGGER.debug(" partId = " + partList.get(i).getId());
				LOGGER.debug(" partName = " + partList.get(i).getName());
				LOGGER.debug(" partNo = " + partList.get(i).getNumber());
				LOGGER.debug(" partParentId = " + partList.get(i).getParentId());
				LOGGER.debug(" partState = " + partList.get(i).getState());
				LOGGER.debug(" partType = " + partList.get(i).getType());
				LOGGER.debug(" partVersion = " + partList.get(i).getVersion());

			// attributes
			LOGGER.debug(" ");
			LOGGER.debug("	！！！ ATTRIBUTES Start ！！！");
			LOGGER.debug(" 	OBJNAME = " + partList.get(i).getOBJNAME());
			LOGGER.debug(" 	DRAWINGNO = " + partList.get(i).getDRAWINGNO());
			LOGGER.debug(" 	COPYRIGHT = " + partList.get(i).getCOPYRIGHT());
			LOGGER.debug(" 	CHARACTERTYPE = "
					+ partList.get(i).getCHARACTERTYPE());
			LOGGER.debug(" 	MASS = " + partList.get(i).getMASS());
			LOGGER.debug(" 	UNITE = " + partList.get(i).getUNITE());
			LOGGER.debug(" 	MODEL = " + partList.get(i).getMODEL());
			LOGGER.debug(" 	DESIGN = " + partList.get(i).getDESIGN());

			LOGGER.debug(" 	PARTTYPE = " + partList.get(i).getPARTTYPE());
			LOGGER.debug(" 	BLANKSPEC = " + partList.get(i).getBLANKSPEC());
			LOGGER.debug(" 	BLANKSTATE = " + partList.get(i).getBLANKSTATE());
			LOGGER.debug(" 	MATERIAL = " + partList.get(i).getMATERIAL());
			LOGGER.debug(" 	MATERIALSTANDARD = "
					+ partList.get(i).getMATERIALSTANDARD());

			LOGGER.debug(" 	DOCUMENTID = " + partList.get(i).getDOCUMENTID());
			LOGGER.debug(" 	DOCUMENTTYPE = "
					+ partList.get(i).getDOCUMENTTYPE());

			LOGGER.debug("	！！！ ATTRIBUTES End ！！！");
			LOGGER.debug(" ");

				LOGGER.debug("！！！ PartBean " + i + " End ！！！");
				LOGGER.debug(" ");
			}
		}
		
		List<DocumentBean> docList = map.get(ParameterUtil.DOCUMENTBEAN);
		if(docList!= null){
			for (int i = 0; i < docList.size(); i++) {
				LOGGER.debug("！！！ DocumentBean " + i + " Start ！！！");
				LOGGER.debug(" docId = " + docList.get(i).getId());
				LOGGER.debug(" docName = " + docList.get(i).getName());
				LOGGER.debug(" docNo = " + docList.get(i).getNumber());
				LOGGER.debug(" docPartId = " + docList.get(i).getPartId());
				LOGGER.debug(" docState = " + docList.get(i).getState());
				LOGGER.debug(" docType = " + docList.get(i).getType());
				LOGGER.debug(" docVersion = " + docList.get(i).getVersion());
				LOGGER.debug(" docLocationRef = " + docList.get(i).getLocationRef());

			// attributes
			LOGGER.debug(" ");
			LOGGER.debug("	！！！ ATTRIBUTES Start ！！！");
			LOGGER.debug(" 	OBJNAME = " + docList.get(i).getOBJNAME());
			LOGGER.debug(" 	MASS = " + docList.get(i).getTITLE());
			LOGGER.debug(" 	MODEL = " + docList.get(i).getMODEL());
			LOGGER.debug(" 	DESIGN = " + docList.get(i).getDESIGN());

			LOGGER.debug(" 	DOCUMENTID = " + docList.get(i).getDOCUMENTID());
			LOGGER.debug(" 	DOCUMENTTYPE = " + docList.get(i).getDOCUMENTTYPE());

			LOGGER.debug("	！！！ ATTRIBUTES End ！！！");
			LOGGER.debug(" ");

				LOGGER.debug("！！！ DocumentBean " + i + " End ！！！");
				LOGGER.debug(" ");
			}
		}
		
		List<EPMDocumentBean> epmDocList = map
				.get(ParameterUtil.EPMDOCUMENTBEAN);
		if(epmDocList != null){
			for (int i = 0; i < epmDocList.size(); i++) {
				LOGGER.debug("！！！ EPMDocumentBean " + i + " Start ！！！");
				LOGGER.debug(" epmDocId = " + epmDocList.get(i).getId());
				LOGGER.debug(" epmDocName = " + epmDocList.get(i).getName());
				LOGGER.debug(" epmDocNo = " + epmDocList.get(i).getNumber());
				LOGGER.debug(" epmDocPartId = " + epmDocList.get(i).getPartId());
				LOGGER.debug(" epmDocState = " + epmDocList.get(i).getState());
				LOGGER.debug(" epmDocType = " + epmDocList.get(i).getType());
				LOGGER.debug(" epmDocVersion = " + epmDocList.get(i).getVersion());
				LOGGER.debug(" epmDocLocationRef = "
						+ epmDocList.get(i).getLocationRef());

			// attributes
			LOGGER.debug(" ");
			LOGGER.debug("	！！！ ATTRIBUTES Start ！！！");
			LOGGER.debug(" 	OBJNAME = " + epmDocList.get(i).getOBJNAME());
			LOGGER.debug(" 	DRAWINGNO = " + epmDocList.get(i).getDRAWINGNO());
			LOGGER.debug(" 	COPYRIGHT = " + epmDocList.get(i).getCOPYRIGHT());
			LOGGER.debug(" 	CHARACTERTYPE = "
					+ epmDocList.get(i).getCHARACTERTYPE());
			LOGGER.debug(" 	MASS = " + epmDocList.get(i).getMASS());
			LOGGER.debug(" 	UNITE = " + epmDocList.get(i).getUNITE());
			LOGGER.debug(" 	MODEL = " + epmDocList.get(i).getMODEL());
			LOGGER.debug(" 	DESIGN = " + epmDocList.get(i).getDESIGN());

			LOGGER.debug(" 	PARTTYPE = " + epmDocList.get(i).getPARTTYPE());
			LOGGER.debug(" 	BLANKSPEC = " + epmDocList.get(i).getBLANKSPEC());
			LOGGER.debug(" 	BLANKSTATE = " + epmDocList.get(i).getBLANKSTATE());
			LOGGER.debug(" 	MATERIAL = " + epmDocList.get(i).getMATERIAL());
			LOGGER.debug(" 	MATERIALSTANDARD = "
					+ epmDocList.get(i).getMATERIALSTANDARD());

			LOGGER.debug("	！！！ ATTRIBUTES End ！！！");
			LOGGER.debug(" ");

				LOGGER.debug("！！！ EPMDocumentBean " + i + " End ！！！");
				LOGGER.debug(" ");
			}
		}
		
		List<PartUsageLinkBean> partLinkList = map
				.get(ParameterUtil.PARTUSAGELINKBEAN);
		if(partLinkList != null){
			for (int i = 0; i < partLinkList.size(); i++) {
				LOGGER.debug("！！！ PartUsageLinkBean " + i + " Start ！！！");
				LOGGER.debug(" parentPartId = "
						+ partLinkList.get(i).getParentPart().getId());
				LOGGER.debug(" childPartId = "
						+ partLinkList.get(i).getChildPart().getId());
				LOGGER.debug("！！！ PartUsageLinkBean " + i + " End ！！！");
				LOGGER.debug(" ");
			}
		}

		List<EPMDescribeLinkBean> epmDocLinkList = map
				.get(ParameterUtil.EPMDESCRIBELINKBEAN);
		if(epmDocLinkList != null){
			for (int i = 0; i < epmDocLinkList.size(); i++) {
				LOGGER.debug("！！！ EPMDescribeLinkBean " + i + " Start ！！！");
				LOGGER.debug(" partId = " + epmDocLinkList.get(i).getPart().getId());
				LOGGER.debug(" epmDocId = "
						+ epmDocLinkList.get(i).getEpmDocument().getId());
				LOGGER.debug("！！！ EPMDescribeLinkBean " + i + " End ！！！");
				LOGGER.debug(" ");
			}
		}
		
		List<DocReferenceLinkBean> docLinkList = map
				.get(ParameterUtil.DOCREFERENCELINKBEAN);
		if(docLinkList != null){
			for (int i = 0; i < docLinkList.size(); i++) {
				LOGGER.debug("！！！ DocReferenceLinkBean " + i + " Start ！！！");
				LOGGER.debug(" partId = " + docLinkList.get(i).getPart().getId());
				LOGGER.debug(" docId = " + docLinkList.get(i).getDocument().getId());
				LOGGER.debug("！！！ DocReferenceLinkBean " + i + " End ！！！");
				LOGGER.debug(" ");
			}
		}
		
		List<EPMMemberLinkBean> epmMemberLinkList = map
				.get(ParameterUtil.EPMMEMBERLINK);
		if(epmMemberLinkList != null){
			for (int i = 0; i < epmMemberLinkList.size(); i++) {
				LOGGER.debug("！！！ EPMMemberLinkBean " + i + " Start ！！！");
				LOGGER.debug(" ParentEpm = " + epmMemberLinkList.get(i).getParentEpm().getLocationRef());
				LOGGER.debug(" ChildEpm = " + epmMemberLinkList.get(i).getChildEpm().getLocationRef());
				LOGGER.debug("！！！ EPMMemberLinkBean " + i + " End ！！！");
				LOGGER.debug(" ");
			}
		}
		
		List<EPMReferenceLinkBean> epmReferenceLinkList = map
				.get(ParameterUtil.EPMREFERENCELINK);
		if(epmReferenceLinkList != null){
			for (int i = 0; i < epmReferenceLinkList.size(); i++) {
				LOGGER.debug("！！！ EPMReferenceLinkBean " + i + " Start ！！！");
				LOGGER.debug(" partId = " + epmReferenceLinkList.get(i).getRefEpmBean().getLocationRef());
				LOGGER.debug(" docId = " + epmReferenceLinkList.get(i).getEpmBean().getLocationRef());
				LOGGER.debug("！！！ EPMReferenceLinkBean " + i + " End ！！！");
				LOGGER.debug(" ");
			}
		}
		
		LOGGER.debug("<<< End printBean");
	}
}
